package day03;

import java.util.Arrays;

public class BubbleSort {
public static void main(String[] args) {
	int [] a = {4,5,3,6,2,1};
	System.out.println("待排序数组 "+Arrays.toString(a));
	int [] b = bubbleSort(a);
	System.out.println("运行后原数组: "+Arrays.toString(a));
	System.out.println("排序后的数组: "+Arrays.toString(b));
}
private static int[] bubbleSort(int [] n) {
	int array[] = new int[n.length];
	for (int k = 0; k < n.length; k++) {
		array[k] = n[k];
	}
	for(int i = 1; i < n.length;i++) {      //做第i趟排序
		for(int j = n.length-1;j > i-1;j--) {     
			if(array[j]<array[j-1]) {       
				int temp=array[j];         //从右边开始比较，小的数往左移，可以看作是气泡从水底（右边）浮上去
				array[j]=array[j-1];
				array[j-1]=temp;
			}
		}
		System.out.println("第"+i+"趟排序 "+Arrays.toString(array));
	}
	return array;
}
}
